/* 注入Vue原型上的方法实体 */
import Vue from 'vue'

// blob流文件下载
Vue.prototype.getBlobFile = (res = {}, name, type = 'xlsx') => {
  let blob = new Blob([res])
  let fileName = `${name}.${type}`
  let link = document.createElement('a')
  link.download = fileName
  link.style.display = 'none'
  link.href = URL.createObjectURL(blob)
  document.body.appendChild(link)
  link.click()
  // 释放URL 对象
  URL.rewvokeObjectURL(link.href)
  document.body.removeChild(link)
}

// location直接下载文件
Vue.prototype.getLocateFile = url => { window.location.href = `${url}` }

// 从数组中删除某个元素
Vue.prototype.removeSingleElem = (arr, item) => {
  if (
    Object.prototype.toString.call(arr) === '[object Array]' &&
    arr.length
  ) {
    let index = arr.indexOf(item)
    if (index > -1) {
      return arr.splice(index, 1)
    }
  }
}

// 初始化form表单
Vue.prototype.initForm = (form, except = false) => {
  for (let key in form) {
    if (except === false || !except.includes(key)) {
      form[key] = ''
    }
  }
  return form
}

// 同步更新form表单值，用于修改回显
Vue.prototype.assignForm = (form, data, prefix = 'yolo_') => {
  for (let key in form) {
    let newKey = `${prefix}` + key
    form[key] = data[newKey]
  }
  return form
}
